package com.fenghongzhang.day005;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    private EditText edit;
    private Button send;
    private LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edit = (EditText) findViewById(R.id.edit);
        send = (Button) findViewById(R.id.send);
        ll = (LinearLayout) findViewById(R.id.ll);



        //默认添加一个fragment
        FragmentManager manager = getSupportFragmentManager();
        final FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.add(R.id.ll,new OneFragment());
        fragmentTransaction.commit();



        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = edit.getText().toString();

                //设置参数
                OneFragment oneFragment = new OneFragment();
                Bundle bundle = new Bundle();
                bundle.putString("name",s);
                oneFragment.setArguments(bundle);

                //替换掉默认的.
                FragmentManager manager1 = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction1 = manager1.beginTransaction();
                //replace就是remove 和add 的结合题
                fragmentTransaction1.replace(R.id.ll,oneFragment);
                fragmentTransaction1.commit();

            }
        });
    }
}
